package com.sq.partner.manager.service.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sq.partner.manager.dao.IGatewayAgentInfoRelaDao;
import com.sq.partner.manager.dao.IGatewayChannelInfoConfigDao;
import com.sq.partner.manager.dao.ITradNoCarProdDao;
import com.sq.partner.manager.dao.ITradNoCarProdPlanDao;
import com.sq.partner.manager.mybatis.model.GatewayUser;
import com.sq.partner.manager.mybatis.model.NocarPlan;
import com.sq.partner.manager.mybatis.model.NocarProd;
import com.sq.partner.manager.mybatis.model.TradAgentInfoRela;
import com.sq.partner.manager.service.IGatewayChannelInfoConfigService;
import com.sq.partner.manager.service.IGatewayClearCacheManager;
import com.sq.partner.manager.service.IGatewayNocarInsureService;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.StrUtil;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
@Service
public class GatewayNocarInsureServiceImpl  implements IGatewayNocarInsureService {
	@Resource
	ITradNoCarProdPlanDao tradNoCarProdPlanDao;
	@Resource
	ITradNoCarProdDao tradNoCarProdInstDao;
	@Resource
	IGatewayChannelInfoConfigDao gatewayChannelInfoConfigDao;
	@Resource
	IGatewayClearCacheManager gatewayClearCacheManager;
	@Resource
	IGatewayChannelInfoConfigService gatewayChannelInfoConfigService;
	@Resource
	IGatewayAgentInfoRelaDao gatewayAgentInfoRelaDao;
	@Override
	public NocarPlan getMaxPlanCodeFromDB(String planFlag) throws SQLException {
		// TODO Auto-generated method stub
		return tradNoCarProdPlanDao.getMaxPlanCodeFromDB( planFlag);
	}
	@Override
	public boolean addNocarPlan(NocarPlan nocarPlan) throws Exception {
		// TODO Auto-generated method stub
		boolean flag =	tradNoCarProdPlanDao.addNocarPlan(nocarPlan);
		// 清除缓存
		//gatewayChannelInfoConfigService.clearAllCacheByChannelCode(nocarPlan.getPartnerCode());
		
		return flag;
	}
	@Override
	public Page<NocarPlan> queryNocarPlanPage(Page<NocarPlan> page)
			throws SQLException {
		// TODO Auto-generated method stub
		return tradNoCarProdPlanDao.queryNocarPlanPage( page);
	}
	@Override
	public NocarPlan queryTradNocarPlanInfo(NocarPlan nocarPlan)
			throws SQLException {
		// TODO Auto-generated method stub
		return tradNoCarProdPlanDao.queryTradNocarPlanInfo(nocarPlan);
	}
	@Override
	public boolean updateTradNocarPlanInfo(NocarPlan nocarPlan)
			throws Exception {
		// TODO Auto-generated method stub
		boolean flag = tradNoCarProdPlanDao.updateTradNocarPlanInfo(nocarPlan);
		// 清除缓存
		//gatewayChannelInfoConfigService.clearAllCacheByChannelCode(nocarPlan.getPartnerCode());
		 
		return flag;
	}
	@Override
	public boolean delTradNocarPlan(String planCode,String partnerCode) throws Exception {
		NocarPlan nocarPlan = new NocarPlan();
		nocarPlan.setPlanCode(planCode);
		NocarPlan oldPlan = tradNoCarProdPlanDao.queryTradNocarPlanInfo(nocarPlan);
		NocarProd prod2 =tradNoCarProdInstDao.queryNocarTradProdInstById(oldPlan.getProdCode());
		//1、合作伙伴代理3、个人代理4、总对总代理
		if("3".equals(prod2.getAgentType())||"4".equals(prod2.getAgentType())){
			//根据方案号删除方案下面配置的代理信息
			gatewayAgentInfoRelaDao.delTradAgentInfoRelaByPlanNo(planCode);
		}
		boolean flag =  tradNoCarProdPlanDao.delTradNocarPlan(planCode);
		// 清除缓存
		//gatewayChannelInfoConfigService.clearAllCacheByChannelCode(partnerCode);
 
		return flag;
	}
	
	@Override
	public Map<String,Object> queryMapTradNocarPlanInfo(NocarPlan nocarPlan)
			throws SQLException {
		return tradNoCarProdPlanDao.queryMapTradNocarPlanInfo(nocarPlan);
	}
	
	@Override
	public boolean updateProxyInfo(String planNo, GatewayUser gatewayuser,
			String agentTypeList) throws Exception {
		boolean flag = false;
		//1、合作伙伴代理3、个人代理4、总对总代理
		if(!StrUtil.isEmpty(agentTypeList)){
			//创建一个JsonParser
			JsonParser parser = new JsonParser();
			//通过JsonParser对象可以把json格式的字符串解析成一个JsonElement对象
			JsonElement el = parser.parse(agentTypeList);
			//把JsonElement对象转换成JsonArray
			JsonArray jsonArray = null;
			if(el.isJsonArray()){
				jsonArray = el.getAsJsonArray();
			}
			String ids="";
			for (JsonElement jsonElement : jsonArray) {
				String id =jsonElement.getAsJsonObject().get("id").getAsString();
				ids= ids+id+",";
			}
			if(ids.length()>0){
				ids=ids.substring(0, ids.length()-1);
			}
			
			flag=gatewayAgentInfoRelaDao.delTradAgentInfoRelaByInsuplannoAndAgentIds(planNo, ids);
			for (JsonElement jsonElement : jsonArray) {
				String id =jsonElement.getAsJsonObject().get("id").getAsString();
				String value=jsonElement.getAsJsonObject().get("value").getAsString();
				if(!StrUtil.isEmpty(id)){
					TradAgentInfoRela tradAgentInfoRela =new TradAgentInfoRela();
					tradAgentInfoRela.setAgentInfoId(Long.valueOf(id));
					tradAgentInfoRela.setInsuPlanNo(planNo);
					if(!StrUtil.isEmpty(value)){
						BigDecimal bigDecimal =new BigDecimal(value);
						bigDecimal=bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP); 
						tradAgentInfoRela.setCommRate(bigDecimal);
					}
					tradAgentInfoRela.setOperTime(new Date());
					tradAgentInfoRela.setOperUser(gatewayuser.getUserid());
					flag = gatewayAgentInfoRelaDao.addOrUpdateTradAgentInfoRela(tradAgentInfoRela);
				}
			}
		}
		return flag;
	}
	
	@Override
	public boolean copyAddTradNocarPlan(NocarPlan nocarPlan, String oldPlanNo,
			boolean isCopy) throws Exception {
		boolean boo= tradNoCarProdPlanDao.addNocarPlan(nocarPlan);
		if(isCopy){
			gatewayAgentInfoRelaDao.copyTradAgentInfoRela(nocarPlan.getPlanCode(), oldPlanNo);
		}
 		//清除缓存
		//gatewayChannelInfoConfigService.clearAllCacheByChannelCode(nocarPlan.getPartnerCode());
		return boo;
	}
	@Override
	public boolean proxyInfoIsEmpty(String planNo) throws Exception {
		boolean flag=false;
		@SuppressWarnings("rawtypes")
		List<Map> list = gatewayAgentInfoRelaDao.queryAgentInfoByInsuPlanNo(planNo);
		if(list==null||list.isEmpty()){
			flag = true;
		}
		return flag;
	}
	@Override
	public  List<Map<String, Object>> searchNoCarPlansByplanCode(Map<String, Object> maps)
			throws Exception {
		// TODO Auto-generated method stub
		 List<Map<String, Object>> nocarPlanLists=tradNoCarProdPlanDao.searchNoCarPlansByplanCode(maps);
		return nocarPlanLists;
	}
	@Override
	public List<Map<String, Object>> impNocarRela(Map<String, Object> map)
			throws Exception {
		 
		return gatewayAgentInfoRelaDao.impNocarRela(map);
	} 
	
}
